package main

import (
	"fmt"
	"sync"
	"time"
)

func main() {
	// define a cond lock with mutex locker
	cond := sync.Cond{L: &sync.Mutex{}}

	sum := 0
	for i := 0; i < 10; i++ {
		go func(idx int) {
			cond.L.Lock()
			defer cond.L.Unlock()
			cond.Wait()
			sum += idx
			fmt.Printf("idx = %d, sum = %d\n", idx, sum)
		}(i)
	}

	time.Sleep(5 * time.Second)
	//for i := 0; i < 10; i++ {
	//	cond.Signal()
	//}

	cond.Broadcast()

	time.Sleep(time.Millisecond)
	fmt.Printf("sum = %d\n", sum)
}
